﻿using System;
using System.Drawing.Imaging;
namespace ERP.Modules
{
    public partial class GenerateBarcodeImage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["d"] != null)
            {
                //Read in the parameters
                var strData = Request.QueryString["d"];
                var imageHeight = Convert.ToInt32(Request.QueryString["h"]);
                var imageWidth = Convert.ToInt32(Request.QueryString["w"]);
                var Forecolor = Request.QueryString["fc"];
                var Backcolor = Request.QueryString["bc"];
                var bIncludeLabel = Request.QueryString["il"].ToLower().Trim() == "true";
                var strImageFormat = Request.QueryString["if"].ToLower().Trim();
                var strAlignment = Request.QueryString["align"].ToLower().Trim();

                var type = BarcodeLib.TYPE.UNSPECIFIED;
                switch (Request.QueryString["t"].Trim())
                {
                    case "UPC-A": type = BarcodeLib.TYPE.UPCA; break;
                    case "UPC-E": type = BarcodeLib.TYPE.UPCE; break;
                    case "UPC 2 Digit Ext": type = BarcodeLib.TYPE.UPC_SUPPLEMENTAL_2DIGIT; break;
                    case "UPC 5 Digit Ext": type = BarcodeLib.TYPE.UPC_SUPPLEMENTAL_5DIGIT; break;
                    case "EAN-13": type = BarcodeLib.TYPE.EAN13; break;
                    case "JAN-13": type = BarcodeLib.TYPE.JAN13; break;
                    case "EAN-8": type = BarcodeLib.TYPE.EAN8; break;
                    case "ITF-14": type = BarcodeLib.TYPE.ITF14; break;
                    case "Codabar": type = BarcodeLib.TYPE.Codabar; break;
                    case "PostNet": type = BarcodeLib.TYPE.PostNet; break;
                    case "Bookland-ISBN": type = BarcodeLib.TYPE.BOOKLAND; break;
                    case "Code 11": type = BarcodeLib.TYPE.CODE11; break;
                    case "Code 39": type = BarcodeLib.TYPE.CODE39; break;
                    case "Code 39 Extended": type = BarcodeLib.TYPE.CODE39Extended; break;
                    case "Code 93": type = BarcodeLib.TYPE.CODE93; break;
                    case "LOGMARS": type = BarcodeLib.TYPE.LOGMARS; break;
                    case "MSI": type = BarcodeLib.TYPE.MSI_Mod10; break;
                    case "Interleaved 2 of 5": type = BarcodeLib.TYPE.Interleaved2of5; break;
                    case "Standard 2 of 5": type = BarcodeLib.TYPE.Standard2of5; break;
                    case "Code 128": type = BarcodeLib.TYPE.CODE128; break;
                    case "Code 128-A": type = BarcodeLib.TYPE.CODE128A; break;
                    case "Code 128-B": type = BarcodeLib.TYPE.CODE128B; break;
                    case "Code 128-C": type = BarcodeLib.TYPE.CODE128C; break;
                    case "Telepen": type = BarcodeLib.TYPE.TELEPEN; break;
                    case "FIM (Facing Identification Mark)": type = BarcodeLib.TYPE.FIM; break;
                    case "Pharmacode": type = BarcodeLib.TYPE.PHARMACODE; break;
                }//switch

                System.Drawing.Image barcodeImage = null;
                try
                {
                    var b = new BarcodeLib.Barcode();
                    if (type != BarcodeLib.TYPE.UNSPECIFIED)
                    {
                        b.IncludeLabel = bIncludeLabel;

                        //alignment
                        switch (strAlignment.ToLower().Trim())
                        {
                            case "c": b.Alignment = BarcodeLib.AlignmentPositions.CENTER;
                                break;
                            case "r": b.Alignment = BarcodeLib.AlignmentPositions.RIGHT;
                                break;
                            case "l": b.Alignment = BarcodeLib.AlignmentPositions.LEFT;
                                break;
                            default: b.Alignment = BarcodeLib.AlignmentPositions.CENTER;
                                break;
                        }//switch

                        if (Forecolor.Trim() == "" && Forecolor.Trim().Length != 6)
                            Forecolor = "000000";//Màu
                        if (Backcolor.Trim() == "" && Backcolor.Trim().Length != 6)
                            Backcolor = "FFFFFF";//Màu

                        //===== Encoding performed here =====
                        barcodeImage = b.Encode(type, strData.Trim(), System.Drawing.ColorTranslator.FromHtml("#" + Forecolor), System.Drawing.ColorTranslator.FromHtml("#" + Backcolor), imageWidth, imageHeight);
                        //===================================

                        //===== Static Encoding performed here =====
                        //barcodeImage = BarcodeLib.Barcode.DoEncode(type, this.txtData.Text.Trim(), this.chkGenerateLabel.Checked, this.btnForeColor.BackColor, this.btnBackColor.BackColor);
                        //==========================================
                        Response.ContentType = "image/" + strImageFormat;
                        var MemStream = new System.IO.MemoryStream();
                        switch (strImageFormat)
                        {
                            case "gif": barcodeImage.Save(MemStream, ImageFormat.Gif); break;
                            case "jpeg": barcodeImage.Save(MemStream, ImageFormat.Jpeg); break;
                            case "png": barcodeImage.Save(MemStream, ImageFormat.Png); break;
                            case "bmp": barcodeImage.Save(MemStream, ImageFormat.Bmp); break;
                            case "tiff": barcodeImage.Save(MemStream, ImageFormat.Tiff); break;
                        }
                        MemStream.WriteTo(Response.OutputStream);
                    }
                }
                catch (Exception ex)
                {
                    HTML.clsFormat.Alert_ERROR("Lỗi",ex.ToString());
                }
                finally
                {
                    if (barcodeImage != null)
                    {
                        barcodeImage.Dispose();
                    }
                }
            }
        }
    }
}